<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>12. Objekte in der PS &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="index.html" />
    <link rel="next" title="13. Aufgaben" href="Aufgaben.html" />
    <link rel="prev" title="11. Pipeline" href="powershell_pipeline.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li class="right" >
          <a href="Aufgaben.html" title="13. Aufgaben"
             accesskey="N">weiter</a> |</li>
        <li class="right" >
          <a href="powershell_pipeline.html" title="11. Pipeline"
             accesskey="P">zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Inhalt</a></h3>
  <ul>
<li><a class="reference internal" href="#">12. Objekte in der PS</a><ul>
<li><a class="reference internal" href="#notwendigkeit-von-objekten">12.1. Notwendigkeit von Objekten</a></li>
<li><a class="reference internal" href="#erzeugen-von-objekten">12.2. Erzeugen von Objekten</a></li>
<li><a class="reference internal" href="#einsatzmoglichkeiten">12.3. Einsatzmöglichkeiten</a><ul>
<li><a class="reference internal" href="#erzeugen-eines-customobjektes-aus-einem-vorhandenen-objekt">12.3.1. Erzeugen eines CustomObjektes aus einem vorhandenen Objekt</a></li>
</ul>
</li>
<li><a class="reference internal" href="#geschwindigkeitsvergleich">12.4. Geschwindigkeitsvergleich</a></li>
</ul>
</li>
</ul>

  <h4>Vorheriges Thema</h4>
  <p class="topless"><a href="powershell_pipeline.html"
                        title="vorheriges Kapitel">11. Pipeline</a></p>
  <h4>Nächstes Thema</h4>
  <p class="topless"><a href="Aufgaben.html"
                        title="nächstes Kapitel">13. Aufgaben</a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/powershell_customobjects.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="objekte-in-der-ps">
<span id="index-0"></span><h1>12. Objekte in der PS<a class="headerlink" href="#objekte-in-der-ps" title="Permalink zu dieser Überschrift">¶</a></h1>
<div class="sidebar">
<p class="first sidebar-title">Zusammenfassung</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Release:</th><td class="field-body">1.0</td>
</tr>
<tr class="field-even field"><th class="field-name">Datum:</th><td class="field-body">01. 05. 2013</td>
</tr>
<tr class="field-odd field"><th class="field-name">Autor:</th><td class="field-body"><strong>Steinam</strong></td>
</tr>
<tr class="field-even field"><th class="field-name">Target:</th><td class="field-body">Schüler FI SYS</td>
</tr>
<tr class="field-odd field"><th class="field-name">status:</th><td class="field-body">In Bearbeitung</td>
</tr>
<tr class="field-even field"><th class="field-name">vollständig:</th><td class="field-body">20 %</td>
</tr>
</tbody>
</table>
<p><strong>Downloads</strong>:</p>
<ul class="simple">
<li><a class="reference download internal" href="_downloads/AB_Objekterzeugung.docx"><tt class="xref download docutils literal"><span class="pre">AB</span> <span class="pre">Objekterzeugung</span></tt></a>.</li>
</ul>
<p class="last"><a class="reference external" href="http://www.petri.co.il/custom-objects-windows-powershell-part-1.htm">http://www.petri.co.il/custom-objects-windows-powershell-part-1.htm</a>
<a class="reference external" href="http://www.petri.co.il/creating-custom-objects-windows-powershell-part-2.htm">http://www.petri.co.il/creating-custom-objects-windows-powershell-part-2.htm</a>
<a class="reference external" href="http://www.petri.co.il/creating-custom-objects-in-windows-powershell-part-3.htm">http://www.petri.co.il/creating-custom-objects-in-windows-powershell-part-3.htm</a>
<a class="reference external" href="http://www.petri.co.il/creating-custom-objects-windows-powershell-part-4.htm">http://www.petri.co.il/creating-custom-objects-windows-powershell-part-4.htm</a>
<a class="reference external" href="http://www.admin-source.de/BlogDeu/463/powershell-eigene-objekte-erstellen-custom-objects">http://www.admin-source.de/BlogDeu/463/powershell-eigene-objekte-erstellen-custom-objects</a>
<a class="reference external" href="http://www.jonathanmedd.net/2011/09/powershell-v3-creating-objects-with-pscustomobject-its-fast.html">http://www.jonathanmedd.net/2011/09/powershell-v3-creating-objects-with-pscustomobject-its-fast.html</a>
<a class="reference external" href="http://blogs.technet.com/b/gary/archive/2009/07/24/using-add-member-in-a-powershell-one-liner-to-create-calculated-properties.aspx?Redirected=true">http://blogs.technet.com/b/gary/archive/2009/07/24/using-add-member-in-a-powershell-one-liner-to-create-calculated-properties.aspx?Redirected=true</a></p>
</div>
<div class="section" id="notwendigkeit-von-objekten">
<h2>12.1. Notwendigkeit von Objekten<a class="headerlink" href="#notwendigkeit-von-objekten" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Die bisherige Arbeit mit der PS legte den Wert auf Elemente der strukturierten Programmierung, z.B. Bedingungen, Schleifen, Funktionen; Werte wurden häufig nur in Form von Variablen bzw. Arrays übergeben.</p>
<p>Die Beschäftigung mit der Pipeline führte den Ansatz weiter fort, indem man nun von <strong>Objekten</strong> spricht, die man nun innerhalb der Pipeline weitergibt.</p>
<p>Objekte setzen die Vielzahl von Informationen zu einem einheitlichen Ganzen zusammen; sie verpacken die bisher in einzelnen Variablen gehaltenen Informationen zu einem sinnvollen Gesamtbild.</p>
<div class="admonition note">
<p class="first admonition-title">Bemerkung</p>
<p><strong>Goldene Regel der PowerShell</strong></p>
<p class="last">Eine Funktion oder ein Cmdlet sollte immer ein Objekt zurückgeben!</p>
</div>
<p>Wenn man kein fertiges Objekt hat und Daten aus verschiedenen Quellen zusammen sammelt, dann muss man sich eben ein eigenes Objekt zusammen bauen!</p>
<p>Das Erzeugen eines eigenen Objektes kann aus vielerlei Weise erfolgen.</p>
</div>
<div class="section" id="erzeugen-von-objekten">
<h2>12.2. Erzeugen von Objekten<a class="headerlink" href="#erzeugen-von-objekten" title="Permalink zu dieser Überschrift">¶</a></h2>
<p id="index-1"><strong>PS 2.0: New-Object mit Add-Member</strong></p>
<p>Dies ist der in PowerShell Version 2.0 vorgesehene Weg um PCCustomObjects zu erstellen.</p>
<p>Ein leeres Objekt erstellen und dann die NoteProperties nachträglich mit dem Cmdlet Add-Member anfügen:</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># Neues leeres Objekt erstellen</span>
<span class="nv">$Obj</span><span class="o">=</span> New-object -TypeName PSObject


<span class="c"># Wert an das Objekt anfügen</span>
Add-Member -InputObject <span class="nv">$Obj</span> -Name Wert1 -Value 1 -MemberType NoteProperty
<span class="c"># Wert an das Objekt anfügen</span>
Add-Member -InputObject <span class="nv">$Obj</span> -Name Wert2 -Value 2 -MemberType NoteProperty
<span class="c"># Wert an das Objekt anfügen</span>
Add-Member -InputObject <span class="nv">$Obj</span> -Name Wert3-Value 3 -MemberType NoteProperty
<span class="c"># CSV Export der Daten</span>
Export-Csv -InputObject <span class="nv">$Obj</span> -Path <span class="s2">&quot;C:\Temp\test.csv&quot;</span> –NoTypeInformation
</pre></div>
</div>
<p><strong>Select-Object</strong></p>
<div class="highlight-sh" id="index-2"><div class="highlight"><pre><span class="nv">$StrObj</span><span class="o">=</span>“” | Select-Object Wert1,Wert2,Wert3
<span class="c"># Werte an die NoteProperties zuweisen</span>

<span class="nv">$StrObj</span>.Wert1 <span class="o">=</span> <span class="s2">&quot;Hello&quot;</span>
<span class="nv">$StrObj</span>.Wert2 <span class="o">=</span> 2
<span class="nv">$StrObj</span>.Wert3 <span class="o">=</span> 3

<span class="nv">$StrObj</span>.GetType<span class="o">()</span>
<span class="nv">$StrObj</span> | Get-Member -Name Wert2
</pre></div>
</div>
<p><strong>New-Module –asCustomObject</strong></p>
<span class="target" id="index-3"></span><p id="index-4">Da alles in PowerShell ein Objekt ist, ist auch ein Modul “nur” ein Objekt mit Methoden (Function) und Feldern (Property).</p>
<p>Deshalb kann man mit dem Cmdlet New-Module sowie dem Parameter –asCustomObject und einem Scriptblock ein Objekt erstellen.</p>
<p>Der Vorteil hierbei ist, dass es viel leichter ist diesem Objekt neuen Methoden (Functions) oder neue Properties anzufügen.</p>
<p>Die bisher gezeigten NoteProperties können jeden Typen annehmen. Die Properties des Objektes, das über New-Modul erstellt wird, können stark Typisiert werden. So dass die Zuweisung eines falschen Wertes in einer Fehlermeldung enden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Objekt</span> <span class="o">=</span> New-Module -AsCustomObject -ScriptBlock <span class="o">{</span>
        <span class="o">[</span>int<span class="o">]</span><span class="nv">$Wert1</span><span class="o">=</span><span class="nv">$null</span>; <span class="c"># Objekt Property als Integer definieren</span>
        Export-ModuleMember -Variable * <span class="c"># Variablen Public machen</span>
<span class="o">}</span>

<span class="nv">$Obj2</span> <span class="o">=</span> New-Module -AsCustomObject -ScriptBlock <span class="o">{</span>

        <span class="o">[</span>int<span class="o">]</span><span class="nv">$Wert1</span> <span class="o">=</span> <span class="nv">$null</span>; <span class="c"># Objekt Property als Integer definieren</span>
        <span class="o">[</span>int<span class="o">]</span><span class="nv">$Wert2</span> <span class="o">=</span> <span class="nv">$null</span>; <span class="c"># Objekt Property als Integer definieren</span>

        <span class="c"># Methode definieren</span>
        Function Add <span class="o">{</span>
                <span class="nv">$Wert1</span>+<span class="nv">$Wert2</span> <span class="c"># Zahlen Addieren</span>
        <span class="o">}</span>

        Export-ModuleMember -Variable * -Function *  <span class="c"># Variablen und Funktionen Public machen</span>
<span class="o">}</span>

<span class="c"># Werte füllen</span>
<span class="nv">$Obj2</span>.Wert1 <span class="o">=</span> 4
<span class="nv">$Obj2</span>.Wert2 <span class="o">=</span> 4
<span class="nv">$Obj2</span>.Add<span class="o">()</span> <span class="c"># Funktion ausführen</span>
</pre></div>
</div>
<p><strong>.NET-Code</strong></p>
<div class="highlight-csharp" id="index-5"><pre># Eine Klasse mit C# Code und Add-Type definieren

add-type @"
using System;
public class myClass{
        public Double number=0;

        public Double Sqr()
        {
                return Math.Pow(number,2.0);
        }
}
"@
# Das vorher definierte Objekt erstellen
$obj = New-Object myClass
# Der Property des Objektes einen Wert zuweisen
$obj.number = 5
# Methode des Objektes ausführen
$obj.Sqr()</pre>
</div>
</div>
<div class="section" id="einsatzmoglichkeiten">
<h2>12.3. Einsatzmöglichkeiten<a class="headerlink" href="#einsatzmoglichkeiten" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Objekte sind immer dann sinnvoll, wenn</p>
<ul class="simple">
<li>man die gegebenen Eigenschaften umbenennen/erweitern will</li>
<li>wenn man mehrere Informationen zusammenfassen und weiterverwenden will</li>
</ul>
<div class="section" id="erzeugen-eines-customobjektes-aus-einem-vorhandenen-objekt">
<h3>12.3.1. Erzeugen eines CustomObjektes aus einem vorhandenen Objekt<a class="headerlink" href="#erzeugen-eines-customobjektes-aus-einem-vorhandenen-objekt" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Im folgenden Beispiel will man die vorhandenen Eigenschaften eines zurückgegebenen Objektes in einer anderen Form darstellen.</p>
<div class="figure">
<img alt="_images/bild1.jpg" src="_images/bild1.jpg" />
</div>
<p>Die Eigenschaft <strong>csname</strong> ist wenig aussagekräftig. Diese und weitere Eigenschaften können durch folgendes Skript geändert werden.</p>
<div class="highlight-sh"><div class="highlight"><pre>get-wmiobject win32_operatingsystem | <span class="sb">`</span>
Select @<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;Computername&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.CSName<span class="o">}}</span>, <span class="sb">`</span>
Version,@<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;OS&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.Caption<span class="o">}}</span>, <span class="sb">`</span>
@<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;LastBoot&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.ConvertToDateTime<span class="o">(</span><span class="nv">$_</span>.LastBootUpTime<span class="o">)}}</span>

<span class="sb">`</span>
</pre></div>
</div>
<p>Der Code in einem Skriptblock kann beliebig sein. So kann die z.B. die LastBootUpTime auch schöner formatiert werden.</p>
<div class="figure">
<img alt="_images/bild2.jpg" src="_images/bild2.jpg" />
</div>
<p>Die Rückgabe des Select-Commandlets ist ein echtes Objekt. Dies erkennt man an dem Nachschalten des Get-Member-CmdLets</p>
<div class="highlight-sh"><div class="highlight"><pre>get-wmiobject win32_operatingsystem | <span class="sb">`</span>
Select @<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;Computername&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.CSName<span class="o">}}</span>, <span class="sb">`</span>
Version,@<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;OS&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.Caption<span class="o">}}</span>, <span class="sb">`</span>
@<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;LastBoot&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.ConvertToDateTime<span class="o">(</span><span class="nv">$_</span>.LastBootUpTime<span class="o">)}}</span> | get-member

<span class="sb">`</span>

TypeName: Selected.System.Management.ManagementObject

Name         MemberType   Definition
----         ----------   ----------
Equals       Method       bool Equals<span class="o">(</span>System.Object obj<span class="o">)</span>
GetHashCode  Method       int GetHashCode<span class="o">()</span>
GetType      Method       <span class="nb">type </span>GetType<span class="o">()</span>
ToString     Method       string ToString<span class="o">()</span>
Computername NoteProperty System.String <span class="nv">Computername</span><span class="o">=</span>STEINAM-LAPTOP
LastBoot     NoteProperty System.DateTime <span class="nv">LastBoot</span><span class="o">=</span>08.04.2013 14:16:08
OS           NoteProperty System.String <span class="nv">OS</span><span class="o">=</span>Microsoft Windows 7 Enterprise
Version      NoteProperty System.String <span class="nv">Version</span><span class="o">=</span>6.1.7601
</pre></div>
</div>
<p>Mit diesem Objekt können nun weitere Dinge getan werden, z.B. das Sortieren nach der Bootzeit</p>
<div class="highlight-sh"><div class="highlight"><pre>get-wmiobject win32_operatingsystem -computername <span class="s2">&quot;localhost&quot;</span>, <span class="s2">&quot;127.0.0.1&quot;</span> | <span class="sb">`</span>
Select @<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;Computername&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.CSName<span class="o">}}</span>, <span class="sb">`</span>
Version,@<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;OS&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.Caption<span class="o">}}</span>, <span class="sb">`</span>
@<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;LastBoot&quot;</span>;<span class="nv">Expression</span><span class="o">={</span><span class="nv">$_</span>.ConvertToDateTime<span class="o">(</span><span class="nv">$_</span>.LastBootUpTime<span class="o">)}}</span> | <span class="sb">`</span>
Sort LastBoot -Descending | format-table –autosize


Computername   Version  OS                              LastBoot
------------   -------  --                              --------
STEINAM-LAPTOP 6.1.7601 Microsoft Windows 7 Enterprise  08.04.2013 14:16:08
STEINAM-LAPTOP 6.1.7601 Microsoft Windows 7 Enterprise  08.04.2013 14:16:08
</pre></div>
</div>
<p>Der Nachteil des Verfahrens ist, dass das Original-Objekt verloren wurde.</p>
<p><strong>Aufgabe</strong></p>
<p>Auf welchem Rechner läuft der Druck-Spooler-Dienst. Zeigen Sie den Namen der Maschine sowie die Anzahl der benötigten Dienste.</p>
<p>Eine erste Annäherung:</p>
<div class="highlight-sh"><div class="highlight"><pre>PS C:<span class="se">\U</span>sers<span class="se">\a</span>dmin&gt; Get-Service Spooler | <span class="k">select </span>machinename, RequiredServices

MachineName                             RequiredServices
-----------                             ----------------
.                                       <span class="o">{</span>HTTP, RPCSS<span class="o">}</span>
</pre></div>
</div>
<p>Falls die Druckspooler-Dienst auf dem eigenen Rechner (.) läuft, soll der Computername ausgegeben werden.</p>
<div class="highlight-sh"><div class="highlight"><pre> Get-Service Spooler | <span class="k">select</span> @<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;Computername&quot;</span>;<span class="nv">Expression</span><span class="o">={</span>
         <span class="k">if</span><span class="o">(</span><span class="nv">$_</span>.Machinename -eq <span class="s2">&quot;.&quot;</span><span class="o">)</span>
         <span class="o">{</span>
                 <span class="nv">$env</span>:COMPUTERNAME

         <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>, RequiredServices

Computername                            RequiredServices
------------                            ----------------
STEINAM-LAPTOP                          <span class="o">{</span>HTTP, RPCSS<span class="o">}</span>
</pre></div>
</div>
<p>Die Anzahl der benötigten Dienste muss noch ermittelt werden. Bisher werden nur die Dienstnamen ausgegeben. Informieren Sie sich dazu über das Measure-Cmdlet.</p>
<div class="highlight-sh"><div class="highlight"><pre> Get-Service Spooler | <span class="k">select</span> @<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;Computername&quot;</span>;<span class="nv">Expression</span><span class="o">={</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$_</span>.Machinename -eq <span class="s2">&quot;.&quot;</span><span class="o">)</span>
 <span class="o">{</span>
     <span class="nv">$env</span>:COMPUTERNAME
 <span class="o">}</span>
 <span class="o">}</span>
 <span class="o">}</span>,
 @<span class="o">{</span><span class="nv">Name</span><span class="o">=</span><span class="s2">&quot;Anzahl Dienste&quot;</span>; <span class="nv">Expression</span><span class="o">=</span> <span class="o">{(</span><span class="nv">$_</span>.RequiredServices | Measure<span class="o">)</span>.count <span class="o">}}</span>, Name, DisplayName


Computername             Anzahl Dienste Name                DisplayName
------------             -------------- ----                -----------
STEINAM-LAPTOP                        2 Spooler             Druckwarteschlange
</pre></div>
</div>
<p>Auch hier geht das ursprüngliche Objekt verloren.
Dies kann durch den nun folgenden Teil vermieden werden.</p>
<p><strong>Hinzufügen neuer Objekteigenschaften mit Hilfe von Add-Member</strong></p>
<p>Auch zu einem bestehenden Objekt können neue Eigenschaften hinzugefügt werden.</p>
<div class="highlight-sh" id="index-6"><div class="highlight"><pre>Get-Service Spooler | Add-Member -MemberType NoteProperty -Name <span class="s2">&quot;Foo&quot;</span> -Value 123 -PassThru
</pre></div>
</div>
<p id="index-7">Mit Hilfe des <strong>passthru</strong>-Parameters wird die neue Eigenschaft mit in die Pipeline geworfen.</p>
<p><strong>Beispiel</strong></p>
<p>Informationen über logische Laufwerke per WMI</p>
<div class="highlight-sh"><div class="highlight"><pre>PS C:<span class="se">\&gt;</span> Get-WmiObject win32_logicaldisk
</pre></div>
</div>
<p>Ein Filter um nur lokale und removable Laufwerke zu sehen:</p>
<blockquote>
<div>PS C:&gt; Get-WmiObject win32_logicaldisk -Filter &#8220;drivetype=2 or drivetype=3&#8221;</div></blockquote>
<p>Alright now that I have this I want to turn it into a nice table showing the name, freespace, and size, but freespace and size are in bytes, which I just cant stand looking at.  Lets use Add-Member to create a couple of script properties and turn them into rounded GB.</p>
<div class="highlight-sh"><div class="highlight"><pre>PS C:<span class="se">\&gt;</span> Get-WmiObject win32_logicaldisk -Filter <span class="s2">&quot;drivetype=2 or drivetype=3&quot;</span> | Add-Member -MemberType ScriptProperty -Name FreeSpaceinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.freespace / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Format-Table Name,FreespaceinGB -AutoSize
</pre></div>
</div>
<p>Lastly I am going to use another add-member cmdlet to add a second property and get closer to my desired result.  I still want to go another step, but first lets just look at the results with two properties added:</p>
<div class="highlight-sh"><div class="highlight"><pre>PS C:<span class="se">\&gt;</span> Get-WmiObject win32_logicaldisk -Filter <span class="s2">&quot;drivetype=2 or drivetype=3&quot;</span> | Add-Member -MemberType ScriptProperty -Name FreeSpaceinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.
freespace / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Add-Member -MemberType ScriptProperty -Name SizeinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.size / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Format-Table name,FreespaceinGB,SizeinGB -AutoSize
</pre></div>
</div>
<p>Und jetzt noch den Anteil des freien Platzes in Prozent</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-WmiObject win32_logicaldisk -Filter <span class="s2">&quot;drivetype=2 or drivetype=3&quot;</span> | Add-Member -MemberType ScriptProperty -Name FreeSpaceinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.freespace / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Add-Member -MemberType ScriptProperty -Name SizeinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.size / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Add-Member -MemberTypeScriptProperty -Name FreespacePercent -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">(([</span>int64<span class="o">]</span><span class="nv">$this</span>.freespace / <span class="o">[</span>int64<span class="o">]</span><span class="nv">$this</span>.size * 100<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Format-Table Name,FreespaceinGB,SizeinGB,FreespacePercent
</pre></div>
</div>
<p>Add-member hat die darunter liegenden .NET-Objekte nicht verändert, sondern um die Eigenschaften erweitert.</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-WmiObject win32_logicaldisk -Filter <span class="s2">&quot;drivetype=2 or drivetype=3&quot;</span> | Add-Member -MemberType ScriptProperty -Name FreeSpaceinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.freespace / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Add-Member -MemberType ScriptProperty -Name SizeinGB -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">((</span><span class="nv">$this</span>.size / 1GB<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Add-Member -MemberType ScriptProperty -Name FreespacePercent -Value <span class="o">{[</span>math<span class="o">]</span>::Round<span class="o">(([</span>int64<span class="o">]</span><span class="nv">$this</span>.freespace / <span class="o">[</span>int64<span class="o">]</span><span class="nv">$this</span>.size * 100<span class="o">)</span>,2<span class="o">)}</span> -PassThru | Get-Member
</pre></div>
</div>
</div>
</div>
<div class="section" id="geschwindigkeitsvergleich">
<h2>12.4. Geschwindigkeitsvergleich<a class="headerlink" href="#geschwindigkeitsvergleich" title="Permalink zu dieser Überschrift">¶</a></h2>
<div class="sidebar" id="index-8">
<p class="first sidebar-title">Unterlagen</p>
<ul class="last simple">
<li><a class="reference download internal" href="_downloads/test2_perf.ps1"><tt class="xref download docutils literal"><span class="pre">AB</span> <span class="pre">Objekterzeugung</span></tt></a>.</li>
</ul>
</div>
<p>PowerShell v3 brings the possibility to create a custom object via</p>
<p>PS V3 bringt die Möglichkeit mit, eigene Objekte mit Hilfe von [pscustomobject] anzulegen.</p>
<p>BeispieL</p>
<div class="highlight-csharp" id="index-9"><pre>$CustomObject2 = [pscustomobject]@{a=1; b=2; c=3; d=4}
$CustomObject2 | Format-List</pre>
</div>
<p>Die Ausgabe von Get-Member zeigt, dass Note-Properties angelegt wurden</p>
<div class="highlight-csharp"><pre>$CustomObject2 | Get-Member


TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
a           NoteProperty System.Int32 a=1
b           NoteProperty System.Int32 b=2
c           NoteProperty System.Int32 c=3
d           NoteProperty System.Int32 d=4</pre>
</div>
<p>Warum sollte man diesen Weg wählen.</p>
<p id="index-10">Im Vergleich zum Anlegen mit Hilfe von Hashtabellen bleibt die Einfügereihenfolge erhalten.</p>
<div class="highlight-csharp"><pre>$HashTableOld = @{a=1; b=2; c=3; d=4}

$HashTableOld

Name                           Value
----                           -----
c                              3
d                              4
b                              2
a                              1</pre>
</div>
<p>Der 2. Grund ist die deutlich erhöhte Geschwindigkeit zu allen anderen Möglichkeiten der Objekterzeugung. Folgendes Beispiel zeigt einen Geschwindigkeitsvergleich.</p>
<div class="highlight-sh"><div class="highlight"><pre>Write-Host <span class="s2">&quot;PS V2 Customobject&quot;</span>

<span class="nv">$TestSelect</span> <span class="o">=</span> <span class="o">{</span>
<span class="o">(</span>0..5000<span class="o">)</span> | ForEach-Object <span class="o">{</span><span class="nv">$CustomObject</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select-Object Name,ID
        <span class="nv">$CustomObject</span>.Name <span class="o">=</span> <span class="s2">&quot;Test Name&quot;</span>
        <span class="nv">$CustomObject</span>.ID <span class="o">=</span> <span class="nv">$_</span>
        <span class="nv">$CustomObject</span>
        <span class="o">}</span>
<span class="o">}</span>
Measure-Command <span class="nv">$TestSelect</span> | Format-Table TotalSeconds -Autosize



Write-Host <span class="s2">&quot;new-object und add-member&quot;</span>

<span class="nv">$TestAddMember</span> <span class="o">=</span> <span class="o">{</span>
<span class="o">(</span>0..5000<span class="o">)</span> | ForEach-Object <span class="o">{</span>
        <span class="nv">$CustomObject</span> <span class="o">=</span> New-Object psobject
        <span class="nv">$CustomObject</span> | Add-Member -Name <span class="s2">&quot;Name&quot;</span> -MemberType NoteProperty -Value <span class="s2">&quot;Test&quot;</span>
        <span class="nv">$CustomObject</span> | Add-Member -Name <span class="s2">&quot;ID&quot;</span>  -MemberType NoteProperty -Value <span class="nv">$_</span>
<span class="nv">$CustomObject</span>
        <span class="o">}</span>
<span class="o">}</span>
Measure-Command <span class="nv">$TestAddMember</span> | Format-Table TotalSeconds -Autosize



Write-Host <span class="s2">&quot;New-Object und hash&quot;</span>


<span class="nv">$TestProperty</span> <span class="o">=</span> <span class="o">{</span>
        <span class="o">(</span>0..5000<span class="o">)</span> | ForEach-Object <span class="o">{</span>New-Object psobject -Property @<span class="o">{</span><span class="nv">Name</span> <span class="o">=</span> <span class="s2">&quot;Test Name&quot;</span>; <span class="nv">ID</span> <span class="o">=</span> <span class="nv">$_</span><span class="o">}}</span>
<span class="o">}</span>
Measure-Command <span class="nv">$TestProperty</span> | Format-Table TotalSeconds -Autosize


Write-Host <span class="s2">&quot;.net-Objekte&quot;</span>


add-type @<span class="s2">&quot;</span>
<span class="s2">using System;</span>
<span class="s2">public class TestObject2{</span>
<span class="s2">        public string name = &quot;&quot;;</span>
<span class="s2">        public int wert = 0;</span>
<span class="s2">}</span>
<span class="s2">&quot;</span>@
Measure-Command <span class="o">{</span>
        0..5000 | ForEach-Object <span class="o">{</span>
                <span class="nv">$dotnet</span> <span class="o">=</span> New-Object TestObject2
                <span class="nv">$dotnet</span>.name <span class="o">=</span> <span class="nv">$_</span>.ToString<span class="o">()</span>
                <span class="nv">$dotnet</span>.wert <span class="o">=</span> <span class="nv">$_</span>
                <span class="nv">$dotnet</span>
        <span class="o">}</span> | Format-Table TotalSeconds -Autosize
<span class="o">}</span>


Write-Host <span class="s2">&quot;pscustomobjects&quot;</span>


<span class="nv">$TestPSCustomObject</span> <span class="o">=</span> <span class="o">{</span>
        <span class="o">(</span>0..5000<span class="o">)</span> | ForEach-Object <span class="o">{[</span>pscustomobject<span class="o">]</span>@<span class="o">{</span><span class="nv">Name</span> <span class="o">=</span> <span class="s2">&quot;Test Name&quot;</span>; <span class="nv">ID</span> <span class="o">=</span> <span class="nv">$_</span><span class="o">}}</span>
<span class="o">}</span>
Measure-Command <span class="nv">$TestPSCustomObject</span> | Format-Table TotalSeconds -Autosize
</pre></div>
</div>
<p>Ein weiteres Skriot vergleicht die Geschwindigkeit beim Erzeugen einer größeren Menge von Objekten anhand dreier unterschiedlicher Wege der Objekterzeugung.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="o">[</span>int<span class="o">[]]</span><span class="nv">$objects</span> <span class="o">=</span> 1,2,5,10
<span class="o">[</span>int<span class="o">]</span><span class="nv">$cycles</span> <span class="o">=</span> 50000
<span class="nv">$report</span> <span class="o">=</span> @<span class="o">()</span>
ForEach <span class="o">(</span><span class="nv">$obj</span> in <span class="nv">$objects</span><span class="o">)</span> <span class="o">{</span>
Switch <span class="o">(</span><span class="nv">$obj</span><span class="o">)</span> <span class="o">{</span>
    1   <span class="o">{</span>
            <span class="nv">$temp</span> <span class="o">=</span> “” | Select Name, Objects,Cycles, Seconds
            Write-Host -fore Green “Beginning performance <span class="nb">test </span><span class="k">for </span>Add-Member custom objects using <span class="nv">$cycles</span> iterations and <span class="k">$(</span><span class="nv">$obj</span><span class="k">)</span> objects”
            <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span>
                        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span>  <span class="o">{</span>
                            <span class="nv">$a</span> <span class="o">=</span> new-Object psobject
                            <span class="nv">$a</span> | add-member -membertype noteproperty -name test1 -value <span class="s2">&quot;test1&quot;</span>
            <span class="o">}})</span>.TotalSeconds
            Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
            <span class="nv">$temp</span>.Name <span class="o">=</span> “Add-Member”
            <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
            <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
            <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
            <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
        <span class="o">}</span>
    2   <span class="o">{</span>
            <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
            Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Add-Member custom objects using $cycles iterations and $($obj) objects&quot;</span>
            <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span>  <span class="o">{</span>
                        <span class="nv">$a</span> <span class="o">=</span> new-Object psobject
                        <span class="nv">$a</span> | add-member -membertype noteproperty -name test1 -value “test1<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test2 -value “test2&quot;</span>
                    <span class="o">}})</span>.TotalSeconds
            Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
            <span class="nv">$temp</span>.Name <span class="o">=</span> “Add-Member”
            <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
            <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
            <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
            <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
        <span class="o">}</span>
    5   <span class="o">{</span>
            <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
            Write-Host -fore Green “Beginning performance <span class="nb">test </span><span class="k">for </span>Add-Member custom objects using <span class="nv">$cycles</span> iterations and <span class="k">$(</span><span class="nv">$obj</span><span class="k">)</span> objects”
            <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span>
                        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span>  <span class="o">{</span>
                            <span class="nv">$a</span> <span class="o">=</span> new-Object psobject
                            <span class="nv">$a</span> | add-member -membertype noteproperty -name test1 -value <span class="s2">&quot;test1&quot;</span>
                            <span class="nv">$a</span> | add-member -membertype noteproperty -name test2 -value <span class="s2">&quot;test2&quot;</span>
                            <span class="nv">$a</span> | add-member -membertype noteproperty -name test3 -value <span class="s2">&quot;test3&quot;</span>
                            <span class="nv">$a</span> | add-member -membertype noteproperty -name test4 -value <span class="s2">&quot;test4&quot;</span>
                            <span class="nv">$a</span> | add-member -membertype noteproperty -name test5 -value <span class="s2">&quot;test5&quot;</span>
                    <span class="o">}})</span>.TotalSeconds
            Write-Host -ForegroundColor Cyan “<span class="k">$(</span><span class="nv">$run</span><span class="k">)</span> seconds”
            <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;Add-Member&quot;</span>
            <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
            <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
            <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
            <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
        <span class="o">}</span>
    10  <span class="o">{</span>
            <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
            Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Add-Member custom objects using $cycles iterations and $($obj) objects&quot;</span>
            <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span>  <span class="o">{</span>
                        <span class="nv">$a</span> <span class="o">=</span> new-Object psobject
                        <span class="nv">$a</span> | add-member -membertype noteproperty -name test1 -value <span class="s2">&quot;test1&quot;</span>
                        <span class="nv">$a</span> | add-member -membertype noteproperty -name test2 -value “test2<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test3 -value &quot;</span>test3<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test4 -value &quot;</span>test4<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test5 -value &quot;</span>test5<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test6 -value &quot;</span>test6<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test7 -value &quot;</span>test7<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test8 -value &quot;</span>test8<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test9 -value &quot;</span>test9<span class="s2">&quot;</span>
<span class="s2">                        $a | add-member -membertype noteproperty -name test10 -value &quot;</span>test10<span class="s2">&quot;</span>
<span class="s2">                    }}).TotalSeconds</span>
<span class="s2">            Write-Host -ForegroundColor Cyan &quot;</span><span class="k">$(</span><span class="nv">$run</span><span class="k">)</span> seconds<span class="s2">&quot;</span>
<span class="s2">            $temp.Name = &quot;</span>Add-Member<span class="s2">&quot;</span>
<span class="s2">            $temp.Objects = $obj</span>
<span class="s2">            $temp.Cycles = $cycles</span>
<span class="s2">            $temp.seconds = $run</span>
<span class="s2">            $report += $temp</span>
<span class="s2">        }</span>
<span class="s2">    }</span>

<span class="s2">    Switch ($obj) {</span>
<span class="s2">    1 {</span>
<span class="s2">        $temp = &quot;&quot; | Select Name, Objects,Cycles, Seconds</span>
<span class="s2">        Write-Host -fore Green &quot;</span>Beginning performance <span class="nb">test </span><span class="k">for </span>Select-Object custom objects using 50000 iterations and <span class="k">$(</span><span class="nv">$obj</span><span class="k">)</span> objects<span class="s2">&quot;</span>
<span class="s2">        $run = $(Measure-Command {for($i = 0;$i -lt 50000;$i++) {</span>
<span class="s2">            $a = 1 |Select-Object test1,test2,test3,test4,test5,test6,test7,test8,test9,test10</span>
<span class="s2">            $a.test1 = &quot;</span>test1<span class="s2">&quot;</span>
<span class="s2">        }}).TotalSeconds</span>
<span class="s2">        Write-Host -ForegroundColor Cyan “$($run) seconds”</span>
<span class="s2">        $temp.Name = &quot;</span>Select-Object<span class="s2">&quot;</span>
<span class="s2">        $temp.Objects = $obj</span>
<span class="s2">        $temp.Cycles = $cycles</span>
<span class="s2">        $temp.seconds = $run</span>
<span class="s2">        $report += $temp</span>
<span class="s2">      }</span>
<span class="s2">    2 {</span>
<span class="s2">        $temp = &quot;&quot; | Select Name, Objects,Cycles, Seconds</span>
<span class="s2">        Write-Host -fore Green &quot;</span>Beginning performance <span class="nb">test </span><span class="k">for </span>Select-Object custom objects using <span class="nv">$cycles</span> iterations and <span class="k">$(</span><span class="nv">$obj</span><span class="k">)</span> objects<span class="s2">&quot;</span>
<span class="s2">        $run = $(Measure-Command {for($i = 0;$i -lt $cycles;$i++) {</span>
<span class="s2">                $a = 1 |Select-Object test1,test2,test3,test4,test5,test6,test7,test8,test9,test10</span>
<span class="s2">                $a.test1 = &quot;</span>test1<span class="s2">&quot;; $a.test2 = &quot;</span>test2<span class="s2">&quot;</span>
<span class="s2">        }}).TotalSeconds</span>
<span class="s2">        Write-Host -ForegroundColor Cyan &quot;</span><span class="k">$(</span><span class="nv">$run</span><span class="k">)</span> seconds<span class="s2">&quot;</span>
<span class="s2">        $temp.Name = &quot;</span>Select-Object<span class="s2">&quot;</span>
<span class="s2">        $temp.Objects = $obj</span>
<span class="s2">        $temp.Cycles = $cycles</span>
<span class="s2">        $temp.seconds = $run</span>
<span class="s2">        $report += $temp</span>
<span class="s2">      }</span>
<span class="s2">    5 {</span>
<span class="s2">        $temp = &quot;&quot; | Select Name, Objects,Cycles, Seconds</span>
<span class="s2">        Write-Host -fore Green &quot;</span>Beginning performance <span class="nb">test </span><span class="k">for </span>Select-Object custom objects using <span class="nv">$cycles</span> iterations and <span class="k">$(</span><span class="nv">$obj</span><span class="k">)</span> objects<span class="s2">&quot;</span>
<span class="s2">        $run = $(Measure-Command {for($i = 0;$i -lt $cycles;$i++) {</span>
<span class="s2">            $a = 1 |Select-Object test1,test2,test3,test4,test5,test6,test7,test8,test9,test10</span>
<span class="s2">            $a.test1 = &quot;</span>test1<span class="s2">&quot;; $a.test2 = &quot;</span>test2<span class="s2">&quot;; $a.test3 = &quot;</span>test3<span class="s2">&quot;; $a.test4 = &quot;</span>test4<span class="s2">&quot;; $a.test5 = &quot;</span>test5<span class="s2">&quot;</span>
<span class="s2">        }}).TotalSeconds</span>
<span class="s2">        Write-Host -ForegroundColor Cyan &quot;</span><span class="k">$(</span><span class="nv">$run</span><span class="k">)</span> seconds<span class="s2">&quot;</span>
<span class="s2">        $temp.Name = &quot;</span>Select-Object<span class="s2">&quot;</span>
<span class="s2">        $temp.Objects = $obj</span>
<span class="s2">        $temp.Cycles = $cycles</span>
<span class="s2">        $temp.seconds = $run</span>
<span class="s2">        $report += $temp</span>
<span class="s2">      }</span>
<span class="s2">    10{</span>
<span class="s2">        $temp = &quot;&quot; | Select Name, Objects,Cycles, Seconds</span>
<span class="s2">        Write-Host -fore Green &quot;</span>Beginning performance <span class="nb">test </span><span class="k">for </span>Select-Object custom objects using <span class="nv">$cycles</span> iterations and <span class="k">$(</span><span class="nv">$obj</span><span class="k">)</span> objects<span class="s2">&quot;</span>
<span class="s2">        $run = $(Measure-Command {for($i = 0;$i -lt $cycles;$i++) {</span>
<span class="s2">            $a = 1 |Select-Object test1,test2,test3,test4,test5,test6,test7,test8,test9,test10</span>
<span class="s2">            $a.test1 = &quot;</span>test1<span class="s2">&quot;; $a.test2 = &quot;</span>test2<span class="s2">&quot;; $a.test3 = &quot;</span>test3<span class="s2">&quot;; $a.test4 = &quot;</span>test4<span class="s2">&quot;; $a.test5 = &quot;</span>test5<span class="s2">&quot;</span>
<span class="s2">            $a.test6 = &quot;</span>test6<span class="s2">&quot;; $a.test7 = &quot;</span>test7<span class="s2">&quot;; $a.test8 = &quot;</span>test8<span class="s2">&quot;; $a.test9 = &quot;</span>test9<span class="s2">&quot;; $a.test10 = “test10&quot;</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> “Select-Object”
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
      <span class="o">}</span>
    <span class="o">}</span>

    Switch <span class="o">(</span><span class="nv">$obj</span><span class="o">)</span> <span class="o">{</span>
    1 <span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
                    New-Object PSObject -Property @<span class="o">{</span>
                    <span class="nv">test1</span> <span class="o">=</span> <span class="s2">&quot;test1&quot;</span>
                    <span class="o">}</span>
                <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;HashTable&quot;</span>
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
      <span class="o">}</span>
    2 <span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
                    New-Object PSObject -Property @<span class="o">{</span>
                      <span class="nv">test1</span> <span class="o">=</span> <span class="s2">&quot;test1&quot;</span>; <span class="nv">test2</span> <span class="o">=</span> <span class="s2">&quot;test2&quot;</span>
                    <span class="o">}</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;HashTable&quot;</span>
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
       <span class="o">}</span>
    5 <span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
            New-Object PSObject -Property @<span class="o">{</span>
            <span class="nv">test1</span> <span class="o">=</span> <span class="s2">&quot;test1&quot;</span>; <span class="nv">test2</span> <span class="o">=</span> <span class="s2">&quot;test2&quot;</span>; <span class="nv">test3</span> <span class="o">=</span> <span class="s2">&quot;test3&quot;</span>; <span class="nv">test4</span> <span class="o">=</span> <span class="s2">&quot;test4&quot;</span>; <span class="nv">test5</span> <span class="o">=</span> <span class="s2">&quot;test5&quot;</span>
        <span class="o">}</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan “<span class="k">$(</span><span class="nv">$run</span><span class="k">)</span> seconds”
        <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;HashTable&quot;</span>
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
      <span class="o">}</span>
    10<span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>| Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
            New-Object PSObject -Property @<span class="o">{</span>
                <span class="nv">test1</span> <span class="o">=</span> <span class="s2">&quot;test1&quot;</span>; <span class="nv">test2</span> <span class="o">=</span> <span class="s2">&quot;test2&quot;</span>; <span class="nv">test3</span> <span class="o">=</span> <span class="s2">&quot;test3&quot;</span>; <span class="nv">test4</span> <span class="o">=</span> <span class="s2">&quot;test4&quot;</span>; <span class="nv">test5</span> <span class="o">=</span> <span class="s2">&quot;test5&quot;</span>
                <span class="nv">test6</span> <span class="o">=</span> <span class="s2">&quot;test6&quot;</span>; <span class="nv">test7</span> <span class="o">=</span> <span class="s2">&quot;test7&quot;</span>; <span class="nv">test8</span> <span class="o">=</span> <span class="s2">&quot;test8&quot;</span>; <span class="nv">test9</span> <span class="o">=</span> <span class="s2">&quot;test9&quot;</span>; <span class="nv">test10</span> <span class="o">=</span> <span class="s2">&quot;test10&quot;</span>
            <span class="o">}</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> “HashTable”
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
       <span class="o">}</span>
    <span class="o">}</span>





    Switch <span class="o">(</span><span class="nv">$obj</span><span class="o">)</span> <span class="o">{</span>
    1 <span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for psCustomObjects  using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
                     <span class="o">[</span>pscustomobject<span class="o">]</span>@<span class="o">{</span><span class="nv">Name</span> <span class="o">=</span> <span class="s2">&quot;Test Name&quot;</span><span class="o">}</span>
                <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;HashTable&quot;</span>
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
      <span class="o">}</span>
    2 <span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
                     <span class="o">[</span>pscustomobject<span class="o">]</span>@<span class="o">{</span><span class="nv">Name</span> <span class="o">=</span> <span class="s2">&quot;Test Name&quot;</span>; <span class="nv">ID</span> <span class="o">=</span> <span class="nv">$i</span><span class="o">}</span>
                    <span class="o">}</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;HashTable&quot;</span>
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
       <span class="o">}</span>
    5 <span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> | Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
            New-Object PSObject -Property @<span class="o">{</span>
            <span class="o">[</span>pscustomobject<span class="o">]</span>@<span class="o">{</span><span class="nv">Name</span> <span class="o">=</span> <span class="s2">&quot;Test Name&quot;</span>; <span class="nv">ID</span> <span class="o">=</span> 1;Test3<span class="o">=</span><span class="s2">&quot;test3&quot;</span>;<span class="nv">Test4</span> <span class="o">=</span> <span class="s2">&quot;Test4&quot;</span>;<span class="nv">Test5</span><span class="o">=</span><span class="s2">&quot;Test5&quot;</span><span class="o">}</span>
        <span class="o">}</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan “<span class="k">$(</span><span class="nv">$run</span><span class="k">)</span> seconds”
        <span class="nv">$temp</span>.Name <span class="o">=</span> <span class="s2">&quot;HashTable&quot;</span>
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
      <span class="o">}</span>
    10<span class="o">{</span>
        <span class="nv">$temp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>| Select Name, Objects,Cycles, Seconds
        Write-Host -fore Green <span class="s2">&quot;Beginning performance test for Hash Table custom objects using $cycles iterations and $($obj) objects&quot;</span>
        <span class="nv">$run</span> <span class="o">=</span> <span class="k">$(</span>Measure-Command <span class="o">{</span><span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$cycles</span>;<span class="nv">$i</span>++<span class="k">)</span> <span class="o">{</span>
            <span class="o">[</span>pscustomobject<span class="o">]</span>@<span class="o">{</span>
                <span class="nv">test1</span> <span class="o">=</span> <span class="s2">&quot;test1&quot;</span>; <span class="nv">test2</span> <span class="o">=</span> <span class="s2">&quot;test2&quot;</span>; <span class="nv">test3</span> <span class="o">=</span> <span class="s2">&quot;test3&quot;</span>; <span class="nv">test4</span> <span class="o">=</span> <span class="s2">&quot;test4&quot;</span>; <span class="nv">test5</span> <span class="o">=</span> <span class="s2">&quot;test5&quot;</span>
                <span class="nv">test6</span> <span class="o">=</span> <span class="s2">&quot;test6&quot;</span>; <span class="nv">test7</span> <span class="o">=</span> <span class="s2">&quot;test7&quot;</span>; <span class="nv">test8</span> <span class="o">=</span> <span class="s2">&quot;test8&quot;</span>; <span class="nv">test9</span> <span class="o">=</span> <span class="s2">&quot;test9&quot;</span>; <span class="nv">test10</span> <span class="o">=</span> <span class="s2">&quot;test10&quot;</span>
            <span class="o">}</span>
        <span class="o">}})</span>.TotalSeconds
        Write-Host -ForegroundColor Cyan <span class="s2">&quot;$($run) seconds&quot;</span>
        <span class="nv">$temp</span>.Name <span class="o">=</span> “HashTable”
        <span class="nv">$temp</span>.Objects <span class="o">=</span> <span class="nv">$obj</span>
        <span class="nv">$temp</span>.Cycles <span class="o">=</span> <span class="nv">$cycles</span>
        <span class="nv">$temp</span>.seconds <span class="o">=</span> <span class="nv">$run</span>
        <span class="nv">$report</span> +<span class="o">=</span> <span class="nv">$temp</span>
       <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
<span class="nv">$report</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li class="right" >
          <a href="Aufgaben.html" title="13. Aufgaben"
             >weiter</a> |</li>
        <li class="right" >
          <a href="powershell_pipeline.html" title="11. Pipeline"
             >zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>